package com.yisu.expressway.api.web.controller;


import com.alibaba.fastjson.JSON;
import com.yisu.expressway.api.domain.config.SystemConfig;
import com.yisu.expressway.ewf.common.Constants;
import com.yisu.expressway.ewf.common.utils.HTTPClientUtil;
import com.yisu.expressway.ewf.common.web.JsonResult;
import com.yisu.expressway.ewf.entity.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ModelAttribute;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import java.util.HashMap;
import java.util.Map;

import static com.yisu.expressway.ewf.common.enums.ResponseCode.SUCCESS;
import static com.yisu.expressway.ewf.common.enums.ResponseCode.TOKEN_NOT_EXIST;

/**
 * Created by LKL on 2016/8/25.
 */
public class BaseController {
    protected final Logger logger = LoggerFactory.getLogger(this.getClass());
    @Resource
    private SystemConfig systemConfig;

    @ModelAttribute
    public void setResponseHeader(HttpServletResponse response) {
        response.setHeader(Constants.REQUEST_RESULT_CODE, String.valueOf(SUCCESS.code));
    }

    public User getUserByToken(String token) {
        Map<String, String> param = new HashMap<>();
        param.put("token", token);
        String json = HTTPClientUtil.doPost(systemConfig.getGetUserInfoApiUrl(), param);
        User user = JSON.parseObject(JSON.parseObject(json).getString("data"), User.class);
        return user;
    }
}
